import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter_bmfmap_example/componets/text/text_field_container.dart';
import 'package:flutter_bmfmap_example/http/http.dart';
import 'package:flutter_bmfmap_example/models/goodmodel.dart';
import 'package:flutter_bmfmap_example/models/orderinfomodel.dart';
import 'package:flutter_bmfmap_example/pages/marketpage/componets/gooditemcard.dart';
import 'package:flutter_bmfmap_example/pages/marketpage/pages/paypage/paypage.dart';
import 'package:flutter_bmfmap_example/service/marketservice.dart';
import 'package:flutter_bmfmap_example/tool/HYSizeFit%20.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:provider/provider.dart';

class SubmitOrdersPage extends StatefulWidget {
  SubmitOrdersPage({Key key, this.content, this.money}) : super(key: key);

  String content;
  double money;

  @override
  _SubmitOrdersPageState createState() => _SubmitOrdersPageState();
}

class _SubmitOrdersPageState extends State<SubmitOrdersPage> {
  var absoluteAddress;
  String intro = '';

  @override
  initState() {
    super.initState();
    // 页面加载时调用 用来开始时访问数据库
    _getinfo();
  }

  _getinfo() async {
    var response = await Http.get(
        'http://xiangrikui.vaiwan.com/userAddress/getAbsoluteAddress');
    this.absoluteAddress = response;
    // print(this.absoluteAddress['name']);
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // backgroundColor: Color.fromRGBO(244, 244, 244, 1),
      appBar: _buildtop(),
      // body: _buildbody()
      body: Column(
        children: [_buildbody()],
      ),
      bottomSheet: _buildbottom(),
    );
  }

  void postShopBuy() async {
    EasyLoading.show(status: '提交订单中');
    var result = await Marketservice.postShopBuy(widget.content, widget.money);
    await Marketservice.postOrderBuy(result.toString(), intro, this.absoluteAddress['id']);
    // 提交订单时需要刷新init
    context.read<Orderinfomodel>().init();
    print(result);
    EasyLoading.dismiss();
    Navigator.pushReplacement(
      context,
      MaterialPageRoute(
        builder: (context) => PayPage(id:result)
      ),
    );

    // Navigator.pushReplacement(context, newRoute)(
    //     context,
    //     new MaterialPageRoute(builder: (context) => new PayPage()),
    //     (route) => route == null,
    //   );
  }

  _buildbottom() {
    return Container(
      height: 80,
      padding: const EdgeInsets.only(left: 16.0, right: 16),
      child: Row(
        children: [
          Text(
            '合计: ',
            style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600),
          ),
          Text('￥ ',
              style: TextStyle(
                fontSize: 14,
                fontWeight: FontWeight.w600,
                color: Color.fromRGBO(186, 36, 27, 1),
              )),
          Text(widget.money.toString(),
              style: TextStyle(
                fontSize: 22,
                fontWeight: FontWeight.w600,
                color: Color.fromRGBO(186, 36, 27, 1),
              )),
          Text(
            ' 免运费',
            style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600),
          ),
          Spacer(),
          InkWell(
            onTap: (){
              postShopBuy();
            },
            child: Container(
              height: 40,
              width: 100,
              decoration: BoxDecoration(
                color: Color.fromRGBO(186, 36, 27, 1),
                borderRadius: BorderRadius.circular(20),
              ),
              child: Center(
                child: Text(
                  '提交订单',
                  style: TextStyle(
                      fontSize: 16,
                      color: Colors.white,
                      fontWeight: FontWeight.w600),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  _buildbody() {
    return Expanded(
      child: ListView(
        physics: const AlwaysScrollableScrollPhysics(
          parent: BouncingScrollPhysics(),
        ),
        children: [
          Padding(
            padding: const EdgeInsets.only(left: 16.0, right: 16),
            child: Container(
              padding: const EdgeInsets.all(8.0),
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(10),
                  boxShadow: [
                    BoxShadow(
                        color: Colors.grey[300],
                        blurRadius: 18,
                        spreadRadius: 0,
                        offset: Offset(0, 6))
                  ]),
              child: Column(
                children: [
                  Row(
                    children: [
                      Container(
                        margin: EdgeInsets.only(right: 4),
                        child: Image.asset(
                          'assets/images/common/logo.png',
                          width: 40,
                          height: 40,
                        ),
                      ),
                      Text(
                        'Nest',
                        style: TextStyle(
                          fontWeight: FontWeight.w500,
                          color: Colors.black,
                        ),
                      ),
                    ],
                  ),
                  Column(
                    children: _buildgoodlist(),
                  ),
                  Padding(
                    padding: const EdgeInsets.only(top: 16.0, bottom: 16),
                    child: Row(children: [
                      Text('配送方式'),
                      Spacer(),
                      Text('快递配送'),
                    ]),
                  ),
                  Padding(
                    padding: const EdgeInsets.only(top: 16.0, bottom: 16),
                    child: Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Text('买家留言'),
                          Spacer(),
                          Container(
                            width: 200,
                            height: 22,
                            child: TextField(
                              //输入文本样式
                              style: TextStyle(
                                  color: Colors.black54, fontSize: 14),
                              //光标样式
                              cursorColor: Color.fromRGBO(196, 183, 215, 1),
                              cursorWidth: 2.0,
                              cursorRadius: Radius.circular(4.0),
                              onChanged: (String text) {
                                // _serach(text);
                                intro = text;
                              },
                              //输入框装饰
                              decoration: InputDecoration(
                                hintText: '请输入留言',
                                //无边框
                                border: InputBorder.none,
                                //填充色为白色
                                fillColor: Colors.white,
                                // filled: true,
                                //圆边框
                                // focusedBorder: OutlineInputBorder(
                                //   borderSide: BorderSide(color: Colors.white),
                                //   borderRadius: BorderRadius.all(
                                //     Radius.circular(15),
                                //   ),
                                // ),
                              ),
                            ),
                          ),
                        ]),
                  ),
                ],
              ),
            ),
          ),
          SizedBox(height: 8),
          Padding(
            padding: const EdgeInsets.only(left: 16.0, right: 16),
            child: Container(
              padding: const EdgeInsets.all(8.0),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(10),
                boxShadow: [
                  BoxShadow(
                    color: Colors.grey[300],
                    blurRadius: 18,
                    spreadRadius: 0,
                    offset: Offset(0, 6),
                  )
                ],
              ),
              child: Column(
                children: [
                  Padding(
                    padding: const EdgeInsets.only(top: 16.0, bottom: 16),
                    child: Row(
                      children: [
                        Text('商品总价'),
                        Spacer(),
                        Text(widget.money.toString()),
                      ],
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.only(top: 16.0, bottom: 16),
                    child: Row(
                      children: [
                        Text('运费'),
                        Spacer(),
                        Text('+￥0.00'),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
          SizedBox(height: 8),
          Padding(
            padding: const EdgeInsets.only(left: 16.0, right: 16),
            child: Container(
              padding: const EdgeInsets.all(8.0),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(10),
                boxShadow: [
                  BoxShadow(
                    color: Colors.grey[300],
                    blurRadius: 18,
                    spreadRadius: 0,
                    offset: Offset(0, 6),
                  )
                ],
              ),
              child: Wrap(
                children: [
                  Text('您购买即视为同意'),
                  Text('《巢用户协议》',
                      style: TextStyle(
                        color: Color.fromRGBO(186, 36, 27, 1),
                      )),
                  Text('《巢商城用户协议》',
                      style: TextStyle(
                        color: Color.fromRGBO(186, 36, 27, 1),
                      )),
                ],
              ),
            ),
          ),
          SizedBox(height: 88),
        ],
      ),
    );
  }

  _buildgoodlist() {
    List<Widget> list = [];
    List<String> shoplist = widget.content.split(';');
    print(widget.content);
    for (int i = 0; i < shoplist.length; i++) {
      if (shoplist[i] != '')
        list.add(
          Padding(
            padding: const EdgeInsets.only(top: 16.0, bottom: 16),
            child: GooditemCard(
              commodityId: shoplist[i].split('x')[0],
              num: shoplist[i].split('x')[1],
            ),
          ),
        );
    }

    return list;
  }

  _buildtop() {
    return PreferredSize(
      preferredSize: Size.fromHeight(170),
      child: Stack(
        children: [
          Container(
            height: 140,
            decoration: BoxDecoration(
              border: Border.all(color: Colors.grey[400], width: 0.5),
              borderRadius: BorderRadius.only(
                bottomLeft: Radius.circular(24),
                bottomRight: Radius.circular(24),
              ),
              gradient: LinearGradient(
                begin: Alignment.topLeft,
                end: Alignment.bottomRight,
                colors: [
                  Color.fromRGBO(234, 204, 132, 1),
                  Color.fromRGBO(217, 177, 90, 1)
                ],
              ),
            ),
          ),
          Column(
            children: [
              SizedBox(
                height: 40,
              ),
              Container(
                width: HYSizeFit.screenWidth,
                child: Stack(
                  alignment: Alignment.center,
                  children: [
                    Positioned(
                      left: 32,
                      child: InkWell(
                        onTap: () {
                          Navigator.pop(context);
                        },
                        child: Icon(
                          Icons.arrow_back_ios,
                          size: 16,
                          color: Colors.white,
                        ),
                      ),
                    ),
                    Container(
                      child: Text(
                        '确定订单',
                        style: TextStyle(
                            fontSize: 20,
                            fontWeight: FontWeight.w600,
                            color: Colors.white),
                      ),
                    ),
                  ],
                ),
              ),
              Padding(
                padding: const EdgeInsets.only(
                    left: 16.0, right: 16, top: 16, bottom: 16),
                child: Container(
                  // height: 40,
                  padding: const EdgeInsets.only(
                      left: 8.0, right: 8, top: 16, bottom: 16),
                  decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(10),
                      boxShadow: [
                        BoxShadow(
                            color: Colors.grey[300],
                            blurRadius: 18,
                            spreadRadius: 0,
                            offset: Offset(0, 6))
                      ]),
                  child: Row(
                    children: [
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Row(
                            children: [
                              Container(
                                margin: EdgeInsets.only(right: 12),
                                child: Text(
                                  this.absoluteAddress == null
                                      ? "---"
                                      : this.absoluteAddress['name'],
                                  style: TextStyle(
                                    fontSize: 16,
                                    fontWeight: FontWeight.w600,
                                  ),
                                ),
                              ),
                              Text(
                                this.absoluteAddress == null
                                    ? "---"
                                    : this.absoluteAddress['phone'].toString(),
                                style: TextStyle(
                                  fontSize: 18,
                                  height: 1.4,
                                  fontWeight: FontWeight.w700,
                                ),
                              )
                            ],
                          ),
                          SizedBox(height: 8),
                          Row(
                            children: [
                              Container(
                                width: 232,
                                child: Text(
                                  this.absoluteAddress == null
                                      ? "---"
                                      : this.absoluteAddress['province'] +
                                          ' ' +
                                          this.absoluteAddress['municipality'] +
                                          ' ' +
                                          this.absoluteAddress['district'] +
                                          ' ' +
                                          this.absoluteAddress['addressIntro'],
                                  maxLines: 2,
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(
                                      fontSize: 13, color: Colors.black87),
                                ),
                              ),
                              // Spacer(),
                            ],
                          ),
                        ],
                      ),
                      Spacer(),
                      Container(
                        child: Icon(
                          Icons.keyboard_arrow_right_sharp,
                          color: Colors.grey[600],
                        ),
                        // onTap: () {
                        //   print('进入收货地址选择页面');
                        // },
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
